package com.babel.util.tools;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 日期工具类
 *
 * @author tangzhi
 */
public class DateTools {

	private static final Log log = LogFactory.getLog(DateTools.class);

	public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_PATTERN_DEFAULTS = "yyyy-MM-dd HH:mm";
	public static final String DATE_PATTERN_DAY = "yyyy-MM-dd";
	public static final String DATEPATTERNDAY = "yyyyMMdd";
	public static final String DATE_PATTERN_MONTH = "yyyy-MM";
	public static final String DAY_LAST_TIME = " 23:59:59";
	private static final String DATE_PATTERN_HOUR = "yyyy-MM-dd HH";


	public static final String DATE_PATTERN_MONTH_ = "yyyyMM";
	public static final String DATE_PATTERN_NUMBER_DAY = "yyyyMMdd";
	public static final String DATE_PATTERN_NUMBER = "yyyyMMddHHmmss";
	public static final String DATE_PATTERN_NUMBER_DAY_CHINESE = "yyyy年MM月dd日";
	public static final long HOUR_TO_MILLISECOND = 60 * 1000;

	public static Date getDate() {
		return new Date();
	}

	/**
	 * 获取上一个月的最大天数
	 *
	 * @return
	 */
	public static int getMaxDayOfLastMonth() {
		Date now = getDate();
		Date lastMonth = DateUtils.addMonths(now, -1);
		lastMonth = getMaxDateOfMonth(lastMonth);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(lastMonth);
		int maxDay = calendar.get(Calendar.DAY_OF_MONTH);
		return maxDay;
	}

	public static int getYearOfLastMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		int yearOfLastMonth = calendar.get(Calendar.YEAR);
		return yearOfLastMonth;
	}

	public static int getMonthOfLastMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		int lastMonth = calendar.get(Calendar.MONTH) + 1;
		return lastMonth;
	}

	public static int getCurrentStatYear() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	// 获取当前月份，cal.get(Calendar.MONTH)是从零开始。
	public static int getCurrentStatMonth() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		return month;
	}

	public static int getCurrentStatDay() {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return day;
	}

	/**
	 * 获取当前小时
	 *
	 * @return
	 */
	public static int getCurrentStatHour() {
		Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 时间校验: 开始时间不能大于当前时间.
	 */
	public static Date validateStartDate(Date startDate) {
		Date today = getDate();
		// 开始时间不能大于当前时间.
		if (startDate.compareTo(today) == 1) {
			log.warn("startDate.compareTo(today)==1, set startDate = today:" + today);
			startDate = today;
		}
		return startDate;
	}

	/**
	 * 时间校验: 不能晚于当前时间(如果晚于当前时间，则替换为当前时间)
	 */
	public static Date notAfterNow(Date myDate) {
		Date today = getDate();
		if (myDate.after(today)) {
			log.warn("myDate.after(today), set myDate = today:" + today);
			myDate = today;
		}
		return myDate;
	}

	/**
	 * 时间校验: 不能晚于昨天(如果晚于昨天，则替换为昨天)
	 */
	public static Date notAfterYesterday(Date myDate) {
		Date today = getDate();
		Date yesterday = DateUtils.addDays(today, -1);
		;
		// 3. 结束时间不能大于昨天.
		if (myDate.after(yesterday)) {
			log.warn("myDate.after(yesterday), set myDate = yesterday:" + yesterday);
			myDate = yesterday;
		}
		return myDate;
	}

	/**
	 * 时间校验: 不能晚于上一个月(如果晚于上一个月，则替换为上一个月)
	 */
	public static Date notAfterLastMonth(Date myDate) {
		Date today = getDate();
		Date lastMonth = DateUtils.addMonths(today, -1);
		lastMonth = DateTools.getMaxDateOfMonth(lastMonth);
		// 3. 结束时间不能大于上一个月.
		if (myDate.after(lastMonth)) {
			log.warn("myDate.after(lastMonth), set myDate = lastMonth:" + lastMonth);
			myDate = lastMonth;
		}
		return myDate;
	}

	/**
	 * 时间校验: 不能晚于上一年(如果晚于上一年，则替换为上一年)
	 */
	public static Date notAfterLastYear(Date myDate) {
		Date today = getDate();
		Date lastYear = DateUtils.addYears(today, -1);
		lastYear = DateTools.getMaxDateOfYear(lastYear);
		// 3. 结束时间不能大于上一年.
		if (myDate.after(lastYear)) {
			log.warn("myDate.after(lastYear), set myDate = lastYear:" + lastYear);
			myDate = lastYear;
		}
		return myDate;
	}

	/**
	 * 时间校验: myDate不能早于basicDate(如果早于basicDate，则替换为basicDate)
	 *
	 * @throws Exception
	 */
	public static Date notBefore(Date myDate, String basicStr) throws Exception {
		Date basicDate = DateTools.stringToDateTime(basicStr);
		// 结束时间不能大于昨天.
		if (myDate.before(basicDate)) {
			log.warn("myDate.before(basicDate), set myDate = basicDate:" + basicDate);
			myDate = basicDate;
		}
		return myDate;
	}

	/***
	 * 将日期转化为字符串。 字符串格式("yyyy-MM-dd HH:mm:ss")。
	 */
	public static String dateTime2String(Date date) {
		return dateToString(date, DATE_PATTERN_DEFAULT);
	}

	/***
	 * 将日期转化为字符串。 字符串格式("yyyy-MM-dd HH:mm")。
	 */
	public static String dateTime3String(Date date) {
		return dateToString(date, DATE_PATTERN_DEFAULTS);
	}

	/***
	 * 将日期转化为字符串。 字符串格式("yyyy-MM-dd")，小时、分、秒被忽略。
	 */
	public static String dateToString(Date date) {
		return dateToString(date, DATE_PATTERN_DAY);
	}
	/***
	 * 将日期转化为字符串。 字符串格式("yyyyMMdd")，小时、分、秒被忽略。
	 */
	public static String dateToString1(Date date) {
		return dateToString(date, DATEPATTERNDAY);
	}
	/***
	 * 将String转化为日期。 字符串格式("yyyyMMdd")，小时、分、秒被忽略。
	 */
	public static Date dateToString2(String date) {
		if(date ==null || date.trim().length()<=0){
			return new Date();
		}
		Date d = strToDateDefault1(date);
		try {
			SimpleDateFormat formater = new SimpleDateFormat(DATE_PATTERN_DEFAULT);
			return formater.parse(formater.format(d));
		} catch (Throwable e) {
			log.error(e);
		}
		return new Date();
	}
	/*public static void main(String[] args) {
		System.out.println(dateTime2String(dateToString2("20170908")));
	}*/
	
	/***
	 * 将日期转化为字符串。 字符串格式("yyyy-MM-dd")，小时、分、秒被忽略。
	 */
	public static String getOrderId(Date date) {
		return dateToString(date, DATE_PATTERN_NUMBER);
	}

	public static String dateToStr(Date date) {
		return dateToString(date, DATE_PATTERN_NUMBER_DAY);
	}

	/***
	 * 将日期转化为字符串
	 */
	public static String dateToString(Date date, String pattern) {
		String str = "";
		try {
			SimpleDateFormat formater = new SimpleDateFormat(pattern);
			if (null != date) {
				str = formater.format(date);
			}
		} catch (Throwable e) {
			log.error(e);
		}
		return str;
	}

	/**
	 * 将传入的年月日转化为Date类型
	 */
	public static Date YmdToDate(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		return calendar.getTime();
	}

	/**
	 * 将字符串转化为日期
	 */
	public static Date stringToDateTime(String str) throws Exception {
		return getDateFormatOfDefault().parse(str);
	}

	/**
	 * 将字符串转化为日期
	 */
	public static Date stringToMediumDateTime(String str) throws ParseException {
		DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM);
		return format.parse(str);
	}

	/**
	 * 获取默认的DateFormat
	 */
	public static DateFormat getDateFormatOfDefault() {
		return new SimpleDateFormat(DATE_PATTERN_DEFAULT);
	}

	/**
	 * 将字符串转化为日期。 字符串格式("YYYY-MM-DD")。
	 * 例如："2012-07-01"或者"2012-7-1"或者"2012-7-01"或者"2012-07-1"是等价的。
	 */
	public static Date stringToDate(String str, String pattern) {
		Date dateTime = null;
		try {
			SimpleDateFormat formater = new SimpleDateFormat(pattern);
			dateTime = formater.parse(str);
		} catch (Exception e) {
			log.error(e);
		}
		return dateTime;
	}

	/**
	 * 转化成默认时间格式 yyyy-MM-dd
	 *
	 * @param str
	 * @return
	 */
	public static Date strToDateDefault(String str) {
		return stringToDate(str, DATE_PATTERN_DAY);
	}
	/**
	 * 转化成默认时间格式 yyyy-MM-dd
	 *
	 * @param str
	 * @return
	 */
	public static Date strToDateDefault1(String str) {
		return stringToDate(str, DATEPATTERNDAY);
	}

	/**
	 * 将字符串转化为日期(从一种格式到另一种格式)。
	 */
	public static String StringPatternToPattern(String str, String pattern1, String pattern2) {
		Date dateTime = null;
		String productStr = "";
		try {
			if (!(str == null || str.equals(""))) {
				SimpleDateFormat formater = new SimpleDateFormat(pattern1);
				dateTime = formater.parse(str);

				SimpleDateFormat formater1 = new SimpleDateFormat(pattern2);
				productStr = formater1.format(dateTime);
			}
		} catch (Exception ex) {
			log.error(ex);
		}
		return productStr;
	}

	/**
	 * 日期时间带时分秒的Timestamp表示
	 */
	public static Timestamp stringToDateHMS(String str) {
		Timestamp time = null;
		try {
			time = java.sql.Timestamp.valueOf(str);
		} catch (Exception ex) {
			log.error(ex);
		}
		return time;

	}

	/**
	 * 取得一个date对象对应的日期的0分0秒时刻的Date对象。
	 */
	public static Date getMinDateOfHour(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
		return calendar.getTime();
	}

	/**
	 * 取得一个date对象对应的日期的0点0分0秒时刻的Date对象。
	 */
	public static Date getMinDateOfDay(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getMinDateOfHour(date));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		return calendar.getTime();
	}

	/**
	 * 取得一年中的最早一天。
	 */
	public static Date getMinDateOfYear(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一年中的最后一天
	 */
	public static Date getMaxDateOfYear(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一周中的最早一天。
	 */
	public static Date getMinDateOfWeek(Date date, Locale locale) {
		if (date == null)
			return null;

		Calendar calendar = Calendar.getInstance();
		int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);

		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		if (locale == null)
			locale = Locale.CHINESE;
		Date tmpDate = calendar.getTime();
		if (Locale.CHINESE.getLanguage().equals(locale.getLanguage())) {
			if (day_of_week == 1) {// 星期天
				tmpDate = DateUtils.addDays(tmpDate, -6);
			} else {
				tmpDate = DateUtils.addDays(tmpDate, 1);
			}
		}

		return tmpDate;
	}

	/**
	 * 取得一周中的最后一天
	 */
	public static Date getMaxDateOfWeek(Date date, Locale locale) {
		if (date == null)
			return null;

		Calendar calendar = Calendar.getInstance();
		int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);

		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		if (locale == null)
			locale = Locale.CHINESE;
		Date tmpDate = calendar.getTime();
		if (Locale.CHINESE.getLanguage().equals(locale.getLanguage())) {
			if (day_of_week == 1) {// 星期天
				tmpDate = DateUtils.addDays(tmpDate, -6);
			} else {
				tmpDate = DateUtils.addDays(tmpDate, 1);
			}
		}

		return tmpDate;
	}

	/**
	 * 取得一月中的最早一天。
	 */
	public static Date getMinDateOfMonth(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一月中的最后一天
	 */
	public static Date getMaxDateOfMonth(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一个date对象对应的日期的23点59分59秒时刻的Date对象。
	 */
	public static Date getMaxDateOfDay(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 取得一个date对象对应的小时的59分59秒时刻的Date对象。
	 */
	public static Date getMaxDateOfHour(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));

		return calendar.getTime();
	}

	/**
	 * 获取2个时间相隔几秒
	 */
	public static int getBetweenSecondNumber(Date startDate, Date endDate) {
		if (startDate == null || endDate == null)
			return -1;

		if (startDate.after(endDate)) {
			Date tmp = endDate;
			endDate = startDate;
			startDate = tmp;
		}

		long timeNumber = -1L;
		long TIME = 1000L;
		try {
			timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;

		} catch (Exception e) {
			log.error(e);
		}
		return (int) timeNumber;
	}

	/**
	 * 获取2个时间相隔几分钟
	 */
	public static int getBetweenMinuteNumber(Date startDate, Date endDate) {
		if (startDate == null || endDate == null)
			return -1;

		if (startDate.after(endDate)) {
			Date tmp = endDate;
			endDate = startDate;
			startDate = tmp;
		}

		long timeNumber = -1l;
		long TIME = 60L * 1000L;
		try {
			timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return (int) timeNumber;
	}

	/**
	 * 获取2个时间相隔几小时
	 */
	public static int getBetweenHourNumber(Date startDate, Date endDate) {
		if (startDate == null || endDate == null)
			return -1;

		if (startDate.after(endDate)) {
			Date tmp = endDate;
			endDate = startDate;
			startDate = tmp;
		}

		long timeNumber = -1l;
		long TIME = 60L * 60L * 1000L;
		try {
			timeNumber = (endDate.getTime() - startDate.getTime()) / TIME;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return (int) timeNumber;
	}

	/**
	 * 获取2个时间相隔几天
	 */
	public static int getBetweenDayNumber(Date startDate, Date endDate) {
		SimpleDateFormat sim1 =  new SimpleDateFormat(DATE_PATTERN_DAY);
		SimpleDateFormat sim2 =  new SimpleDateFormat(DATE_PATTERN_DAY);
		 String startDateStr =  sim1.format(startDate);
		 String endDateStr = sim2.format(endDate);
		
		 try {
			startDate = sim1.parse(startDateStr);
			endDate = sim1.parse(endDateStr);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if (startDate == null || endDate == null)
			return -1;

		if (startDate.after(endDate)) {
			Date tmp = endDate;
			endDate = startDate;
			startDate = tmp;
		}

		long dayNumber = -1L;
		long DAY = 24L * 60L * 60L * 1000L;
		try {
			// "2010-08-01 00:00:00 --- 2010-08-03 23:59:59"算三天
			dayNumber = (endDate.getTime() + 1000 - startDate.getTime()) / DAY;

		} catch (Exception e) {
			log.error(e);
		}
		return (int) dayNumber;
	}

	/**
	 * 获取2个时间相隔几月
	 */
	public static int getBetweenMonthNumber(Date startDate, Date endDate) {
		int result = 0;
		try {
			if (startDate == null || endDate == null)
				return -1;

			// swap start and end date
			if (startDate.after(endDate)) {
				Date tmp = endDate;
				endDate = startDate;
				startDate = tmp;
			}

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);

			int monthS = calendar.get(Calendar.MONTH);
			int yearS = calendar.get(Calendar.YEAR);

			calendar.setTime(endDate);
			int monthE = calendar.get(Calendar.MONTH);
			int yearE = calendar.get(Calendar.YEAR);

			if (yearE - yearS == 0) {
				result = monthE - monthS;
			} else {
				result = (yearE - yearS - 1) * 12 + (12 - monthS) + monthE;
			}

		} catch (Exception e) {
			log.error(e);
		}
		return result;
	}

	/**
	 * 获取2个时间相隔几年
	 */
	public static int getBetweenYearNumber(Date startDate, Date endDate) {
		int result = 0;
		try {
			if (startDate == null || endDate == null)
				return -1;

			// swap start and end date
			if (startDate.after(endDate)) {
				Date tmp = endDate;
				endDate = startDate;
				startDate = tmp;
			}

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);
			int yearS = calendar.get(Calendar.YEAR);

			calendar.setTime(endDate);
			int yearE = calendar.get(Calendar.YEAR);

			result = yearE - yearS;

		} catch (Exception e) {
			log.error(e);
		}
		return result;
	}

	/**
	 * 按天拆分时间
	 */
	public static List<Date> splitDateByDay(Date startDate, Date endDate) {
		if (startDate == null || endDate == null)
			return null;

		List<Date> dateList = new ArrayList<Date>();
		dateList.add(startDate);

		int num = getBetweenDayNumber(startDate, endDate);
		for (int i = 1; i <= num; i++) {
			dateList.add(DateUtils.addDays(startDate, i));
		}

		return dateList;
	}

	/**
	 * 按月拆分时间
	 */
	public static List<Date> splitDateByMonth(Date startDate, Date endDate) {
		List<Date> dateList = new ArrayList<Date>();

		if (startDate == null || endDate == null) {
			return dateList;
		}

		dateList.add(startDate);
		int num = getBetweenMonthNumber(startDate, endDate);
		for (int i = 1; i <= num; i++) {
			dateList.add(DateUtils.addMonths(startDate, i));
		}

		return dateList;
	}

	/**
	 * 按年拆分时间
	 */
	public static List<Date> splitDateByYear(Date startDate, Date endDate) {
		List<Date> dateList = new ArrayList<Date>();

		if (startDate == null || endDate == null) {
			return dateList;
		}

		dateList.add(startDate);
		int num = getBetweenYearNumber(startDate, endDate);
		for (int i = 1; i <= num; i++) {
			dateList.add(DateUtils.addYears(startDate, i));
		}

		return dateList;
	}

	/**
	 * 本季度
	 */
	public static List<Date> getCurrentQuarter() {
		List<Date> dateList = new ArrayList<Date>();
		Date date = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);// 一月为0

		dateList.add(1, calendar.getTime());// 结束时间设置为当前时间

		if (month >= 0 && month <= 2) {// 第一季度
			calendar.set(Calendar.MONTH, 0);
		} else if (month >= 3 && month <= 5) {// 第二季度
			calendar.set(Calendar.MONTH, 3);
		} else if (month >= 6 && month <= 8) {// 第三季度
			calendar.set(Calendar.MONTH, 6);
		} else {// 第四季度
			calendar.set(Calendar.MONTH, 9);
		}

		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		dateList.add(0, calendar.getTime());

		return dateList;
	}

	/**
	 * 上季度
	 */
	public static List<Date> getLastQuarter() {
		List<Date> dateList = new ArrayList<Date>();
		Date date = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);// 一月为0

		// 如果是第一季度则返回去年的第四季度
		if (month >= 0 && month <= 2) {// 当前第一季度
			calendar.add(Calendar.YEAR, -1);// 退到去年
			calendar.set(Calendar.MONTH, 9);// 去年十月
		} else if (month >= 3 && month <= 5) {// 当前第二季度
			calendar.set(Calendar.MONTH, 0);
		} else if (month >= 6 && month <= 8) {// 当前第三季度
			calendar.set(Calendar.MONTH, 3);
		} else {// 当前第四季度
			calendar.set(Calendar.MONTH, 6);
		}
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);

		dateList.add(0, calendar.getTime());

		calendar.add(Calendar.MONTH, 3);// 加3个月，到下个季度的第一天
		calendar.add(Calendar.DAY_OF_MONTH, -1);// 退一天，得到上季度的最后一天
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);

		dateList.add(1, calendar.getTime());

		return dateList;
	}

	/**
	 * 返回2个日期中的大者
	 */
	public static Date max(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return null;
		}
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.after(date2)) {
			return date1;
		} else {
			return date2;
		}
	}

	/**
	 * 返回不大于date2的日期 如果 date1 >= date2 返回date2 如果 date1 < date2 返回date1
	 */
	public static Date ceil(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return null;
		}
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.after(date2)) {
			return date2;
		} else {
			return date1;
		}
	}

	/**
	 * 返回不小于date2的日期 如果 date1 >= date2 返回date1 如果 date1 < date2 返回date2
	 */
	public static Date floor(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return null;
		}
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.after(date2)) {
			return date1;
		} else {
			return date2;
		}
	}

	/**
	 * 返回2个日期中的小者
	 */
	public static Date min(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return null;
		}
		if (date1 == null) {
			return date2;
		}
		if (date2 == null) {
			return date1;
		}
		if (date1.after(date2)) {
			return date2;
		} else {
			return date1;
		}
	}

	/**
	 * 判断输入日期是否是一天中的最大时刻
	 */
	public static boolean isMaxDayOfDay(Date date1, String precision) {
		if (date1 == null)
			return false;
		Date date2 = getMaxDateOfDay(date1);
		int diffNum = 0;
		if ("HH".equals(precision)) {
			diffNum = getBetweenHourNumber(date1, date2);
		} else if ("mm".equals(precision)) {
			diffNum = getBetweenMinuteNumber(date1, date2);
		} else {
			diffNum = getBetweenSecondNumber(date1, date2);
		}
		return diffNum == 0;
	}

	/**
	 * 判断输入日期是否是一天中的最小时刻
	 */
	public static boolean isMinDayOfDay(Date date1, String precision) {
		if (date1 == null)
			return false;
		Date date2 = getMinDateOfDay(date1);
		int diffNum = 0;
		if ("HH".equals(precision)) {
			diffNum = getBetweenHourNumber(date1, date2);
		} else if ("mm".equals(precision)) {
			diffNum = getBetweenMinuteNumber(date1, date2);
		} else {
			diffNum = getBetweenSecondNumber(date1, date2);
		}
		return diffNum == 0;
	}

	/**
	 * 判断输入日期是否是一天中的最大时刻
	 */
	public static boolean isMaxDayOfDay(Date date1) {
		if (date1 == null)
			return false;
		Date date2 = getMaxDateOfDay(date1);
		int secondNum = getBetweenSecondNumber(date1, date2);
		return secondNum == 0;
	}

	/**
	 * 判断输入日期是否是一天中的最小时刻
	 */
	public static boolean isMinDayOfDay(Date date1) {
		if (date1 == null)
			return false;
		Date date2 = getMinDateOfDay(date1);
		int secondNum = getBetweenSecondNumber(date1, date2);
		return secondNum == 0;
	}

	/**
	 * 判断输入日期是否是一月中的最大时刻
	 */
	public static boolean isMaxDayOfMonth(Date date1) {
		if (date1 == null)
			return false;
		Date date2 = getMaxDateOfMonth(date1);
		int secondNum = getBetweenSecondNumber(date1, date2);
		return secondNum == 0;
	}

	/**
	 * 判断输入日期是否是一月中的最小时刻
	 */
	public static boolean isMinDayOfMonth(Date date1) {
		if (date1 == null)
			return false;
		Date date2 = getMinDateOfMonth(date1);
		int secondNum = getBetweenSecondNumber(date1, date2);
		return secondNum == 0;
	}

	/**
	 * 输入日期是否为同一天.
	 */
	public static boolean isTheSameDay(Date startDate, Date endDate) {
		String startDateStr = dateToString(startDate);
		String endDateStr = dateToString(endDate);
		return startDateStr.equals(endDateStr);
	}

	/**
	 * 功能：获取昨天最大时间。 输入: 2010-01-31 00:00:00 返回：2010-01-30 23:59:59
	 */
	public static Date getLastMaxDay(Date startDate) {
		if (startDate == null) {
			return null;
		}
		startDate = DateUtils.addDays(startDate, -1);
		return DateTools.getMaxDateOfDay(startDate);
	}

	/**
	 * 功能：获取昨天最小时间。 输入: 2010-01-31 00:00:00 返回：2010-01-30 00:00:00
	 */
	public static Date getLastMinDay(Date startDate) {
		if (startDate == null) {
			return null;
		}
		startDate = DateUtils.addDays(startDate, -1);
		return DateTools.getMinDateOfDay(startDate);
	}

	/**
	 * 根据字符串时间,返回Calendar
	 */
	public static Calendar getCalendar(String datetimeStr) {
		Calendar cal = Calendar.getInstance();
		if (StringUtils.isNotBlank(datetimeStr)) {
			Date date = DateTools.stringToDate(datetimeStr, DATE_PATTERN_DEFAULT);
			cal.setTime(date);
		}
		return cal;
	}

	/**
	 * startStr 或者 startStr-endStr
	 */
	public static String getDifferentStr(String startStr, String endStr) {
		String dateRangeStr = "";
		if (startStr.equals(endStr)) {
			dateRangeStr = startStr;
		} else {
			dateRangeStr = startStr + "-" + endStr;
		}
		return dateRangeStr;
	}

	/**
	 * 给定一个日期和天数，得到这个日期+天数的日期
	 *
	 * @author tangzhi,2012-11-28
	 * @param date
	 *            指定日期
	 * @param day
	 *            天数
	 * @return
	 */
	public static String getNextDay(String date, int num) {
		Date d = stringToDate(date, DATE_PATTERN_DAY);
		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(d);

		int day = ca.get(Calendar.DATE);
		day = day + num;
		ca.set(Calendar.DATE, day);
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DAY);

	}

	/**
	 * 根据指定格式获取日期数据
	 *
	 * @author tangzhi,2012-11-28
	 * @param date
	 *            ：指定日期
	 * @param pattern
	 *            ：日期格式
	 * @return
	 */
	private static String getFormatDateTime(Date date, String pattern) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		return format.format(date);
	}

	/**
	 * 获取给定日期的下一个月的日期的最晚时间
	 *
	 * @param startDate
	 * @return
	 */
	public static Date getNextMonthDay(Date startDate) {
		// 是不是
		// int month = startDate.getMonth();
		Date monthEndDate = getMaxDateOfMonth(startDate);
		Date nextMonth = DateUtils.addMonths(startDate, 1);
		nextMonth = stringToDate(dateToString(nextMonth, DATE_PATTERN_DAY) + DAY_LAST_TIME, DATE_PATTERN_DEFAULT);
		if (isTheSameDay(startDate, monthEndDate)) {
			nextMonth = getMaxDateOfMonth(nextMonth);
		}
		return nextMonth;
	}

	/**
	 * 获取给定日期的下几个月的日期的最晚时间
	 *
	 * @param startDate
	 * @return
	 */
	public static Date getNextManyMonthDay(Date startDate, int month) {
		// 是不是
		// int month = startDate.getMonth();
		Date monthEndDate = getMaxDateOfMonth(startDate);
		Date nextMonth = DateUtils.addMonths(startDate, month);
		nextMonth = stringToDate(dateToString(nextMonth, DATE_PATTERN_DAY) + DAY_LAST_TIME, DATE_PATTERN_DEFAULT);
		if (isTheSameDay(startDate, monthEndDate)) {
			nextMonth = getMaxDateOfMonth(nextMonth);
		}
		return nextMonth;
	}

	/**
	 * 获取几秒后/前的时间
	 *
	 * @param date
	 * @param seconds
	 * @return
	 */
	public static Date getNewDate(Date date, int seconds) {
		long millSeconds = date.getTime();
		millSeconds += seconds * 1000;
		return new Date(millSeconds);
	}

	/**
	 * 给定一个日期和秒，得到这个日期+秒的日期
	 *
	 * @author tangzhi,2012-11-28
	 * @param date
	 *            指定日期
	 * @param day
	 *            天数
	 * @return
	 */
	public static String getNextSecond(Date date, int seconds) {

		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(date);

		int second = ca.get(Calendar.SECOND);
		second = second + seconds;
		ca.set(Calendar.SECOND, second);

		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DEFAULT);

	}

	/**
	 * 给定一个日期和秒，得到这个日期+秒的日期(Date类型)
	 *
	 * @param date
	 *            指定日期
	 * @param day
	 *            天数
	 * @return Date
	 */
	public static Date getNextSecondToDate(Date date, int seconds) {
		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(date);
		int second = ca.get(Calendar.SECOND);
		second = second + seconds;
		ca.set(Calendar.SECOND, second);
		return ca.getTime();
	}

	/**
	 * 给定一个时间和天数，得到这个时间+天数的日期
	 *
	 * @author luoyi,2014-10-5
	 * @param date
	 *            指定时间
	 * @param day
	 *            天数
	 * @return
	 */
	public static String getNextDaytime(String date, int num) {
		Date d = stringToDate(date, DATE_PATTERN_DEFAULT);
		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(d);

		int day = ca.get(Calendar.DATE);
		day = day + num;
		ca.set(Calendar.DATE, day);
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DEFAULT);

	}

	/**
	 * +天返回“yyyy-MM-dd”格式字符串
	 *
	 * @param date
	 * @param num
	 * @return
	 */
	public static String getAddDaytime(String date, int num) {
		Date d = stringToDate(date, DATE_PATTERN_DAY);
		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(d);

		int day = ca.get(Calendar.DATE);
		day = day + num;
		ca.set(Calendar.DATE, day);
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DAY);

	}

	public static String getFrontDaytime(int num) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, -num);
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DAY);
	}

	public static Date getFrontDay(int num) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, -num);
		return ca.getTime();
	}

	public static String getNowDate() {
		Calendar ca = Calendar.getInstance();
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DAY);
	}

	public static String getNowtime() {
		Calendar ca = Calendar.getInstance();
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DEFAULT);
	}

	public static String getNowtime2() {
		Calendar ca = Calendar.getInstance();
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_NUMBER);
	}

	public static String getFrontMonthtime(int num) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, -num);
		return getFormatDateTime(ca.getTime(), DATE_PATTERN_DAY);
	}

	public static String GetDateTimeFormatString(String formatter) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat(formatter);
		return fmt.format(c.getTime());
	}

	public static String getFrontMonthDate(String date, int num) {
		SimpleDateFormat formater = new SimpleDateFormat(DATE_PATTERN_DAY);
		try {
			Date dateTime = formater.parse(date);
			Calendar c = Calendar.getInstance();
			c.setTime(dateTime);
			c.add(Calendar.MONTH, num);
			return formater.format(c.getTime());
		} catch (Exception e) {
			return "";
		}
	}

	public static long getMicTimes(String time) {
		SimpleDateFormat fmat = new SimpleDateFormat(DATE_PATTERN_DEFAULT);
		Date dateTime = null;
		try {
			dateTime = fmat.parse(dateToString(getDate()) + " " + time);
		} catch (Exception e) {
			log.error(e);
		}
		return dateTime.getTime();
	}

	public static int getTimeDist(String time) {
		int distTime = 0;
		try {
			SimpleDateFormat fmat = new SimpleDateFormat(DATE_PATTERN_DEFAULT);
			Date dateTime = fmat.parse(dateToString(getDate()) + " " + time);
			distTime = (int) (dateTime.getTime() - getDate().getTime());
		} catch (Exception e) {
			log.error(e);
		}
		return distTime;
	}

	public static boolean isPastdue(String date, int timeSize) throws Exception {
		long now = System.currentTimeMillis();
		long time = stringToDateTime(date).getTime();
		if ((now - time) > (timeSize * HOUR_TO_MILLISECOND)) {
			return true;
		}
		return false;
	}

	/**
	 * lxl比较当前时间和传入时间加上年数的大小， 传入时间compareDate大 返回true,否则返回false
	 *
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static boolean compareCurrentAndLastYear(Date compareDate, Date needDate, int addYear) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(compareDate);
		cal.add(Calendar.YEAR, addYear);
		compareDate = cal.getTime();
		int i = compareDate.compareTo(needDate);
		if (i >= 0) {
			return true;
		}
		return false;
	}

	public static Date getTomorrowHourAndMinuteAndSecond(int hour, int minute, int seconds) {
		Date today = getDate();
		Date tomorrow = DateUtils.addDays(today, 1);
		Date tomorrowSetMinute = DateUtils.setMinutes(tomorrow, minute);
		Date tomorrowSetSeconds = DateUtils.setSeconds(tomorrowSetMinute, seconds);
		return DateUtils.setHours(tomorrowSetSeconds, hour);
	}

	public static boolean isGt(String date) {
		try {
			Date endDate = stringToDateTime(date);
			Date nowDate = getDate();
			if (endDate.compareTo(nowDate) == 1) {
				return true;
			}
		} catch (Exception e) {
		}
		return false;
	}

	// 将字符串转为时间戳
	public static Date getTime(String user_time) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long time = new Long(user_time);
		String d = format.format(time);
		Date date = format.parse(d);
		return date;
	}

	// 将时间戳转为字符串
	public static String getStrTime(String cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		// 例如：cc_time=1291778220
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
	}

	/**
	 * 18岁成年时间校验 birthDay yyyyMMdd 成年返回true(生日当天为false)
	 * @param birthDay
	 * @return
	 */
	public static Boolean isGrown_up(String birthDay) {
		try {
			Date birthday = DateTools.stringToDate(birthDay + "235959", DateTools.DATE_PATTERN_NUMBER);
			Date today = new Date();
			today = DateUtils.addYears(today, -18);
			return today.after(birthday);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 给定一个日期和秒，得到这个日期+秒的日期(Date类型)
	 *
	 * @param date
	 *            指定日期
	 * @param day
	 *            天数
	 * @return Date
	 */
	public static Date getNextSecond2(Date date, int seconds) {
		Calendar ca = Calendar.getInstance();
		ca.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		ca.setTime(date);
		int second = ca.get(Calendar.SECOND);
		second = second + seconds;
		ca.set(Calendar.SECOND, second);
		return ca.getTime();
	}
	/**
	 * 根据时间,返回Calendar
	 */
	public static Calendar getCalendar(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}
	/**
	 * 获传入时间date的年份
	 */
	public static int getYear(Date date) {
		Calendar cal = getCalendar(date);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 是否是闰年
	 * @return
	 */
	public static boolean isLeapYear(Date date){
		int year = getYear(date);
		return (year% 4 == 0 && year % 100 != 0) || (year% 400 == 0);
	}
	public static Date addMonths(Date date, int amount) {
		return add(date, 2, amount);
	}
	public static Date add(Date date, int calendarField, int amount) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(calendarField, amount);
		return c.getTime();
	}
	/**
	 * 给定一个时间得到这个时间是周几
	 * @param pTime
	 * @return
	 */
	public static int dayForWeek(String pTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(pTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}
	/**
	 * 给定一个时间得到这个时间是周几
	 * @param pTime
	 * @return
	 */
	public static String dayForWeek(Date pTime) {
		Calendar c = Calendar.getInstance();
		c.setTime(pTime);
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		String day = "";
		switch (dayForWeek) {
			case 1:
				day+="周一";
				break;
			case 2:
				day+="周二";
				break;
			case 3:
				day+="周三";
				break;
			case 4:
				day+="周四";
				break;
			case 5:
				day+="周五";
				break;
			case 6:
				day+="周六";
				break;
			case 7:
				day+="周日";
				break;
		}
		return day;
	}
	/**
	 * 给定一个时间得到这个时间的时+分 如： 08:59
	 * @return
	 */
	public static String getTimeByHourAndMin(Date d){
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		return format.format(d);
	}
	/**
	 * 给定一个时间得到这个时间的几月几日 如： 08-05
	 * @return
	 */
	public static String getTimeByMonthAndDay(Date d){
		SimpleDateFormat format = new SimpleDateFormat("MM-dd");
		return format.format(d);
	}
	/**
	 * 根据一个时间得到这个时间所在的月份 如果是当月的话就显示本月
	 * @param d
	 * @return
	 */
	public static String getMonth(Date d){
		SimpleDateFormat format = new SimpleDateFormat("MM");
		SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
		String checkMonth = format.format(d);
		int year = Integer.valueOf(yearFormat.format(d));
		String nowMonth = format.format(new Date());
		Calendar now = Calendar.getInstance();  
	    int a =  now.get(Calendar.YEAR); //今年的时间
	    if(a==year){//同样的年份的时候
	    	return checkMonth.equals(nowMonth)?"本月": checkMonth+"月";
	    }else
	    	return year+"年"+ checkMonth+"月";
	}
	
	public static int getMonthByInt(Date d){
		SimpleDateFormat format = new SimpleDateFormat("MM");
		String nowMonth = format.format(new Date());
		return Integer.valueOf(nowMonth);
	}
	public static int getDayByInt(Date d){
		SimpleDateFormat format = new SimpleDateFormat("dd");
		String nowMonth = format.format(d);
		return Integer.valueOf(nowMonth);
	}
	public static int getHousByInt(Date d){
		//HH:mm:ss
		SimpleDateFormat format = new SimpleDateFormat("HH");
		String nowMonth = format.format(d);
		return Integer.valueOf(nowMonth);
	}
	
	public static int getMinByInt(Date d){
		//HH:mm:ss
		SimpleDateFormat format = new SimpleDateFormat("mm");
		String nowMonth = format.format(d);
		return Integer.valueOf(nowMonth);
	}
	public static int getSecondByInt(Date d){
		//HH:mm:ss
		SimpleDateFormat format = new SimpleDateFormat("ss");
		String nowMonth = format.format(d);
		return Integer.valueOf(nowMonth);
	}
	/**
	 * 获取三个月前的第一天日期
	 */
	public static Date getThreeMonth(Date d){
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.MONTH, -3);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		Date aDate=calendar.getTime();
		return aDate;
	}

	/**
	 *  转换时间格式
	 * @param date 日期
	 * @return STRING
	 * @auther 杨康
	 */
	public static String formatCH(String date) {
		if (date == null)
			return "";
		SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_PATTERN_DAY);        // 实例化模板对象
		SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_PATTERN_NUMBER_DAY_CHINESE);        // 实例化模板对象
		Date d = null;
		try {
			d = sdf1.parse(date);   // 将给定的字符串中的日期提取出来
		} catch (Exception e) {            // 如果提供的字符串格式有错误，则进行异常处理
			e.printStackTrace();       // 打印异常信息
		}
		return sdf2.format(d);
	}

	/**
	 * 获取当前时间2年后的时间
	 * @auther 杨康
	 * @return Date
	 */
	public static Date twoYearsTime() {
		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 2);
		return curr.getTime();
	}
	public static Date tenOldYearsTime() {
		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.YEAR, curr.get(Calendar.YEAR)- 10);
		return curr.getTime();
	}
	
	/**
	 * 获取时间格式为 yyyy-MM-dd HH
	 * @auther 杨康
	 * @return String
	 */
	public static String getByHour() {
		return format(new Date(), DATE_PATTERN_HOUR);
	}

	/**
	 * 生成对应的格式时间
	 *
	 * @param date   时间
	 * @param format 格式
	 * @return str
	 * @auther 杨康
	 */
	public static String format(Date date, String format) {
		DateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	public static String getNYR(Date date) {
		return format(date, DATE_PATTERN_DAY);
	}
	public static String getNY(Date date) {
		return format(date, DATE_PATTERN_MONTH_);
	}

	public static Date str2date(String str) throws ParseException {
		return new SimpleDateFormat(DATE_PATTERN_DAY).parse(str);
	}
	public static Date getAgoDay(Date date){
		Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        date = calendar.getTime();  
        return date;
	}
	/**
	 * 计算p2p到期时间
	 * @param startDate
	 * @param days
	 * @param month
	 * @return
	 */
	public static Date getP2PDueDate(String investProduct,Date startDate,Integer days ,Integer month ){
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DEFAULT);
		if(startDate==null){
			return null;
		}else if(month !=null){
			Date now;
			try {
				now = sdf.parse(sdf.format(startDate));
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(now);
				calendar.add(Calendar.MONTH, month);
				if(isNowDayInterest(investProduct)){
					return getAgoDay(calendar.getTime());
				}
				return calendar.getTime();
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(days!=null){
			try {
				if(isNowDayInterest(investProduct)){
					return  getAgoDay(sdf.parse(getNextDaytime(sdf.format(startDate), days)));
				}
				return  sdf.parse(getNextDaytime(sdf.format(startDate), days));
			} catch (ParseException e) {
				e.printStackTrace();
			}	
		}
		return null;
	}
	/**
	 * 计算p2p投资期限
	 * @param startDate
	 * @param days
	 * @param month
	 * @return
	 */
	public static Integer getP2PTermInvest(String investProduct,Date startDate,Date endDate ){
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_DAY);
		try {
			startDate = sdf.parse(sdf.format(startDate));
			endDate = sdf.parse(sdf.format(endDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int days = (int) ((endDate.getTime()-startDate.getTime())/(24*60*60*1000));
		if(isNowDayInterest(investProduct)){
			days = days+1;
		}
		
		return  days;  
	}
	/**
	 *  计息时间是否从当日开始
	 * @param productType 产品类型
	 * @return
	 */
	private static boolean isNowDayInterest(String productType){
		if("MORE_DAY".equals(productType) || "YUEMANYING".equals(productType) || "APP_MORE_DAY".equals(productType) || "TIYANJIN".equals(productType)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 日期工具类方法：
	 * 两个日期之间的天的计算
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getBetweenDay(Date startDate, Date endDate) {
		if (startDate == null || endDate == null) {
			return -1;
		}
		if (startDate.after(endDate)) {
			Date tmp = endDate;
			endDate = startDate;
			startDate = tmp;
		}
		long dayNumber = -1L;
		long DAY = 24L * 60L * 60L * 1000L;
		Calendar tempCal = Calendar.getInstance(); 
		Calendar startCal = Calendar.getInstance();
		tempCal.setTime(startDate);
		startCal.set(tempCal.get(Calendar.YEAR),tempCal.get(Calendar.MONTH),tempCal.get(Calendar.DATE),0,0,0);

		Calendar endCal = Calendar.getInstance();
		tempCal.setTime(endDate);
		endCal.set(tempCal.get(Calendar.YEAR),tempCal.get(Calendar.MONTH),tempCal.get(Calendar.DATE),0,0,0);
		try {
			dayNumber = (endCal.getTime().getTime() - startCal.getTime().getTime()) / DAY;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return (int) dayNumber;
	}
	
}
